package com.lak.nagivation.cal;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lak.nagivation.bean.CalResult;
import com.lak.nagivation.bean.Environments;
import com.lak.nagivation.bean.Road;
import com.lak.nagivation.bean.Truck;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.ArrayList;
import java.util.List;


public class RoutePlan {
    @Autowired
    private String URL_Driving;

    public String getURL(String origin, String destination) {
        String urlString = AmapServer.getHeadOfDriving() + "origin=" + origin + "&" + "destination=" + destination + "&extensions=all&output=JSON&key=" + AmapServer.getKEY();
        return AmapServer.requestULR(urlString);
    }

    public static CalResult getRoad(String start, String end){
        String[] roads;
        String[] polyline;
        String[] instruction;
        String[] step_path;
        List<String> List_roads = new ArrayList<>();
        List<String> List_polyline = new ArrayList<>();
        List<String> List_instruction = new ArrayList<>();
        List<String> List_step_path = new ArrayList<>();

        String res= new RoutePlan().getURL(start,end);
        JSONObject jsonObject = JSON.parseObject(res);
        JSONObject route=jsonObject.getJSONObject("route");
        JSONArray paths=route.getJSONArray("paths");
        JSONObject path=paths.getJSONObject(0);
        JSONArray steps=path.getJSONArray("steps");
        for (int i = 0; i < steps.size(); i++) {
            JSONObject step=steps.getJSONObject(i);
            String road=step.getString("road");
            System.out.println(road);
            if (road==null){
                road="";
            }
            String PP=step.getString("polyline");
            String[] pline=PP.split(";");

            PP=PP.replaceAll(";",",");
            String[] lnglat=PP.split(",");
            for (int j = 0; j < lnglat.length; j++) {
                List_polyline.add(lnglat[j]);
            }
            List_step_path.add(PP);
            List_roads.add(road);
            List_instruction.add(step.getString("instruction"));
        }
        System.out.println(jsonObject);

        roads=List_roads.toArray(new String[List_roads.size()]);
        polyline=List_polyline.toArray(new String[List_polyline.size()]);
        instruction=List_instruction.toArray(new String[List_instruction.size()]);
        step_path=List_step_path.toArray(new String[List_step_path.size()]);

        String[] getInfo=CoreCal.pretreatment_Instruction(instruction,step_path);
//      String[] roadsss={"","南焦路","顺平路","回民营桥"};
//      Road[] RS=CoreCal.pretreatment_Road(roadsss);
        Road[] RS=CoreCal.pretreatment_Road(roads,step_path);
        Truck truck=new Truck();
        Environments env=new Environments();
        String[] polyline_de=CoreCal.pretreatment_Polyline(polyline);

        String[] check1=GetDll.checkPass1(truck, RS, env);
        String[] check2=GetDll.checkPass2(truck, RS, env,polyline_de);
        String[] check3=GetDll.checkPass3(truck, RS, env,polyline_de);
        String[] de_check1=CoreCal.de_check1(check1[2],step_path);

        CalResult result=new CalResult();
        result.setStatus_result(de_check1);
        result.setCurve_result(check2);
        result.setJunction_result(check3);
        result.setFeature_result(getInfo);
        result.setPolyline_de_result(polyline_de);
        result.setStep_path(step_path);
        result.setName(1);

//        JSONObject RESULT=new JSONObject();
        return  result;
    }


    public static void main(String[] args) {
        String start_point = "116.5178660836933,40.09969855992148";
        String end_point = "116.4750390321562,40.08493457297443";
        getRoad(start_point,end_point);
    }


}

